<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);

$SIZE = 50;
$page = MiscUtils::getParam('p', 1);
$order = MiscUtils::getParam('o', 't.order');
$queue = MiscUtils::getParam('q', 'ASC');
$items = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'admin_hm_categories', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d;

$root = new stdClass();
$root->id = 0;
$root->name = 'ROOT';
$root->parent_id = -1;
__root($items, $root);

function __root($a, $n) {
  for ($i = 0, $il = count($a); $i < $il; $i++) {
    $tmp = $a[$i];
    if ($tmp->parent_id == $n->id) {
      $tmp->_c = array();
      $n->_c[] = $tmp;
      __root($a, $tmp);
    }
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  padding: 0;
  margin: 0;
}
div.container {
  width: 96%;
  height: 355px;
  border: 3px solid #ddd;
}
table.list {
  font-size: 12px;
  width: 100%;
}
table.list td.title {
  padding: 0 5px;
  height: 25px;
  font: bold 15px Arial;
  color: #333;
}
table.list td.allcat {
  padding: 0 5px;
  height: 25px;
  text-align: right;
  width: 50px;
}
table.list td.allcat a {
  font: normal 10px Trebuchet MS;
  color: #666;
  text-decoration: none;
}
table.list td.allcat a:hover {
  text-decoration: underline;
}
table.list td.subtitle {
  padding: 0 5px;
  height: 25px;
  border-top: 1px solid #eee;
}
table.list td.subtitle a {
  font: bold 11px Verdana;
  color: #0092D2;
  text-decoration: none;
}
table.list td.subtitle a:hover {
  text-decoration: underline;
}
table.list td.content {
  padding: 0 5px 2px 5px;
  height: 35px;
  font: normal 11px Arial;
}
table.list td.content a {
  font: normal 11px Arial;
  color: #666;
  text-decoration: none;
}
table.list td.content a:hover {
  text-decoration: underline;
}
table.list a.nohover:hover {
  text-decoration: none !important;
}
ul.hztl {
  width: 100%;
  height: 38px;
  overflow: hidden;
}
ul.hztl li {
  display: inline;
  vertical-align: top;
}
ul.hztl a {
  margin-right: 5px;
  white-space: nowrap;
}
</style>
</head>
<body>
<div class="container">
  <table class="list" cellpadding=0 cellspacing=0>
    <tr>
      <td class="title">Categories</td>
      <td class="allcat"><a href="#" onclick="parent.location.href='http://<?php echo URL . "?i=" . CAT_ID; ?>';">Show all</a></td>
    </tr>
    <?php foreach ($root->_c as $idx => &$cat) { ?>
    <tr><td class="subtitle" colSpan=2><a href="#" <?php if ($cat->category_id != 0) { ?>onclick="parent.location.href='http://<?php echo URL . "?i=" . CAT_ID . "&cat=$cat->category_id"; ?>';"<?php } else { ?>class="nohover"<?php } ?>><?php echo $cat->name; ?></a></td></tr>
    <tr>
      <td class="content" colSpan=2>
        <ul class="hztl">
        <?php foreach ($cat->_c as $idx2 => &$child) { ?>
        <li><a href="#" <?php if ($child->category_id != 0) { ?>onclick="parent.location.href='http://<?php echo URL . "?i=" . CAT_ID . "&cat=$child->id"; ?>';"<?php } else { ?>class="nohover"<?php } ?>><?php echo $child->name; ?></a></li>
        <?php } ?>
        </ul>
      </td>
    </tr>
    <?php } ?>
  </table>
</div>
</body>
</html>